package com.study.services;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.DeptMAO;
import com.study.model.mdao.PermissionMAO;
import com.study.model.pojo.Dept;
import com.study.model.pojo.User;

@Service
@Transactional(rollbackOn = Exception.class)
public class PermissionServices {
	
	@Autowired
	PermissionMAO perDAO;
	/**
	 * 根据权限id查询对应的角色
	 * @param perid
	 * @return
	 */
	public List<JSONObject> getFindRole(int perid) {
		return perDAO.selePersionrole(perid);
	}
	
	/**
	 * 获取所有权限信息
	 * @return
	 */
	public List<JSONObject> getFindPersstion() {
		return perDAO.selectPersionnAll();
	}
	
	/**
	 * 根据角色id查询对应的权限
	 * @param rolid
	 * @return 
	 */
	public List<JSONObject> getfidnPerstionroleid(String rolid) {
		return perDAO.selectRoleperstionroid(Integer.parseInt(rolid));
	}
	
	/**
	 * 查找所有角色和所对应的权限
	 * @return
	 */
	public List<JSONObject> getfindRolePeAll(){
		return perDAO.selectRolepersstion();
	}
	/**
	 * 删除权限对应的角色
	 * @param rolid
	 */
	public void setdelectRolepersstion(String perid) {
		perDAO.delectPerstionRole(perid);
	}
	public List<JSONObject> setfinurl() {
		return perDAO.selectUrl();
	}
	
	public JSONObject setlectUrlcs(String urlcus) {
		return perDAO.selectUrlurls(urlcus);
	}
	
	/**
	 * 根据角色查询出用对应的权限
	 * @param rolid
	 * @return
	 */
	public List<JSONObject> getfindPersstion(String rolid) {
		System.out.println("roles            "+rolid);
		List<JSONObject> selectRoleperstionroid = perDAO.selectRoleperstionroid(Integer.parseInt(rolid));
		String strs="";
		List<JSONObject> list=new ArrayList<>();
		for (JSONObject jsonObject : selectRoleperstionroid) {
			System.out.println(jsonObject.getString("per_id")+"            per_id");
			List<JSONObject> selectPersstionSupperid = perDAO.selectPersstionSupperid(jsonObject.getString("per_id"));
			if(selectPersstionSupperid.size()>0) {
				for (JSONObject string : selectPersstionSupperid) {
					JSONObject obj=new JSONObject();
					obj.put("xm", string.getString("per_id"));
					list.add(obj);
				}
			}else {
				JSONObject obj=new JSONObject();
				obj.put("xm", jsonObject.getString("per_id"));
				
				list.add(obj);
			}			
		}
		
		System.out.println(list+"         JSONObject");
		return list;

	}
}
